package com.mz.ieann.aide.dialog

import androidx.fragment.app.FragmentActivity
import com.mz.ieann.aide.R
import com.mz.ieann.aide.base.vb.BaseVBDialog
import com.mz.ieann.aide.databinding.DialogWarningLayoutBinding
import com.mz.ieann.dokit.utils.LogUtils

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz90
 * @date on 2021 2021/11/14 13:39
 */
class WarningDialog private constructor(
    private val message: String,
    private val left: String? = null,
    private val right: String? = null,
    private val ok: () -> Unit = {  }
) : BaseVBDialog(R.layout.dialog_warning_layout) {

    private val binding by bindView<DialogWarningLayoutBinding>()

    override fun initCreate() {
        binding.warningMsg.text = message
        binding.warningSure.text = left ?: resources.getString(R.string.dialog_sure)
        binding.warningCancel.text = right ?: resources.getString(R.string.dialog_cancel)

        bindSure(binding.warningSure) {
            ok()
        }
        bindCancel(binding.warningCancel)
    }

    class Builder {

        private var message: String = ""
        private var leftText: String? = null
        private var rightText: String? = null

        fun setMessage(msg: String?): Builder {
            message = msg ?: ""
            return this@Builder
        }

        fun setLeft(left: String?): Builder {
            leftText = left
            return this@Builder
        }

        fun setRight(right: String?): Builder {
            rightText = right
            return this@Builder
        }

        fun create(ok: ()-> Unit = {  }) = WarningDialog(message, leftText, rightText, ok)
    }

    fun show(activity: FragmentActivity?) {
        activity?.apply {
            if (supportFragmentManager.findFragmentByTag("warningDialog") != null || dialog?.isShowing == true) {
                LogUtils.e("dialog already showing")
                return
            }
            super.show(this, "warningDialog")
        } ?: LogUtils.e("activity can not be null")
    }
}